import re
from util import dig_dns
from multiprocessing.dummy import Pool
from functools import partial
import json
import sys
# 读取已有的 dns
try:
    dns_list = json.load('dns.json')
except:
    dns_list = []

def get_ips(file_name):
    with open(file_name,'rt')as f:
        text = f.read()
    ret = re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',text)
    return ret

def my_dig_dns(ip):
    if dig_dns('@' + ip, 'www.baidu.com'):
        return ip


#dns_list.extend(get_ips('nameservers-all.txt'))
dns_list.extend(get_ips('cook.txt'))
print(len(set(dns_list)))

pool = Pool(300)
ret_list = pool.map(my_dig_dns,dns_list)
pool.close()
pool.join()

pass_dns = []
for ret in ret_list:
    if ret:
        pass_dns.append(ret)

print(pass_dns)
assert(len(sys.argv)<3)
print(len(pass_dns))
with open('dns.json','wt')as f:
    json.dump(pass_dns,f)
